﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using zyconfig;

namespace zymain
{
    public partial class FrmCgysSh : Form
    {
        private int i = 0;
        private int csgd =0;
        public int _kjm=1;
        public List<string[]> list = new List<string[]>();
        public FrmCgysSh()
        {
            InitializeComponent();
            csgd = this.groupBox1.Location.Y+90;
        }
        public void AddConfig(int kjm)
        {
            this._kjm = kjm;
            this.AddDbList(0);
            if(i>0)
                this.jsBtn.Enabled = true;
            else
                this.jsBtn.Enabled = false;
        }
        private void AddDbList(int i)
        {
            ComboBox c = this.groupBox1.Controls.Find("combox1" + i, true)[0] as ComboBox;
            ((ComboBox)c).Items.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "select 库室名称,虚拟库室 from 虚拟库室 where  虚拟库室>0 order by 配置优先级";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        ((ComboBox)c).Items.Add(dRow["库室名称"].ToString().Trim());
                        //this.XnkList.Add(int.Parse(dRow["虚拟库室"].ToString()));
                    }
                }
                if (((ComboBox)c).Items.Count > 0)
                {
                    ((ComboBox)c).SelectedIndex = 0;
                    //this.ld();
                }
            }
        }
        private void ld()
        {
            for (int j = 0; j <= i; j++)
            {
                Console.WriteLine("combox1" + j);
                ComboBox c = this.groupBox1.Controls.Find("combox1" + j, true)[0] as ComboBox;
                ComboBox c1 = this.groupBox1.Controls.Find("combox2" + j, true)[0] as ComboBox;
                //Console.WriteLine(c.SelectedItem.ToString()+":"+c.SelectedIndex);
                string ksm = c.SelectedItem.ToString().Trim();
                c1.Items.Clear();
                using (DbOperHandler _open = new Common().Doh())
                {
                    _open.Reset();
                    _open.ConditionExpress = "库室名称='" + ksm + "'";
                    string xnk = _open.GetField("虚拟库室", "虚拟库室").ToString().Trim();
                    _open.Reset();
                    _open.ConditionExpress = "库键码=" + this._kjm;
                    string kfks = _open.GetField("书目数据库定义", "可用分配库室").ToString().Trim();
                    string[] kfs = kfks.Split(' ');
                    string kss = "";
                    foreach (string str in kfs)
                    {
                        kss += "'" + str + "',";
                    }
                    string sql = " select 馆藏地址,部门名称 from 馆藏地址定义 where 虚拟库室=" + xnk;
                    if (!string.IsNullOrEmpty(kss))
                    {
                        kss = kss.Substring(0, kss.Length - 1);
                        sql += " and 馆藏地址 in(" + kss + ") ";
                    }
                    sql += " order by 序号";
                    _open.Reset();
                    _open.SqlCmd = sql;
                    DataTable dt = _open.GetDataTable();
                    if (dt.Rows.Count > 0)
                    {
                        foreach (DataRow dRow in dt.Rows)
                        {
                            c1.Items.Add(dRow["部门名称"].ToString().Trim());
                        }
                    }
                    if (c1.Items.Count > 0)
                    {
                        c1.SelectedIndex = 0;
                    }
                }
            }
        }
        private void combox_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.ld();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            i++;
            this.groupBox1.Size = new Size(this.groupBox1.Width, this.groupBox1.Height + 35);
            this.Size = new Size(this.Width, this.Height + 35);
            TextBox t1 = new TextBox();
            t1.Location = new Point(6, csgd);
            t1.Name = "text" + i;
            t1.Text = "A-D999";
            t1.Size = new Size(155, 30);
            this.groupBox1.Controls.Add(t1);
            ComboBox c1 = new ComboBox();
            c1.Location = new Point(180, csgd);
            c1.Name = "combox1" + i;
            c1.Size = new Size(150, 30);
            c1.SelectedIndexChanged += new EventHandler(this.combox_SelectedIndexChanged);
            this.groupBox1.Controls.Add(c1);
            ComboBox c2 = new ComboBox();
            c2.Location = new Point(350, csgd);
            c2.Name = "combox2" + i;
            c2.Size = new Size(150, 30);
            this.groupBox1.Controls.Add(c2);
            csgd += 35;
            Console.WriteLine("i csgd:" + i + ":" + csgd);
            this.AddDbList(i);
            this.jsBtn.Enabled = true;
        }

        private void jsBtn_Click(object sender, EventArgs e)
        {
            if (i > 0)
            {
                this.jsBtn.Enabled = true;
                TextBox t = this.groupBox1.Controls.Find("text" + i, true)[0] as TextBox;
                ComboBox c = this.groupBox1.Controls.Find("combox1" + i, true)[0] as ComboBox;
                ComboBox c1 = this.groupBox1.Controls.Find("combox2" + i, true)[0] as ComboBox;
                this.groupBox1.Controls.Remove(t);
                this.groupBox1.Controls.Remove(c);
                this.groupBox1.Controls.Remove(c1);
                i--;
                csgd = csgd - 35;
                this.groupBox1.Size = new Size(this.groupBox1.Width, this.groupBox1.Height - 35);
                this.Size = new Size(this.Width, this.Height - 35);
                Console.WriteLine("i csgd:" + i + ":" + csgd);
            }
            else

                this.jsBtn.Enabled = false;
        }

        private void FrmCgysSh_Load(object sender, EventArgs e)
        {
            this.AddDbList(0);
        }

        private void saveBtn_Click(object sender, EventArgs e)
        {
            using (DbOperHandler _open = new Common().Doh())
            {
                for (int j = 0; j <= i; j++)
                {
                    TextBox t = this.groupBox1.Controls.Find("text" + j, true)[0] as TextBox;
                    ComboBox c = this.groupBox1.Controls.Find("combox1" + j, true)[0] as ComboBox;
                    ComboBox c1 = this.groupBox1.Controls.Find("combox2" + j, true)[0] as ComboBox;
                    _open.Reset();
                    _open.ConditionExpress = "库室名称='" + c.SelectedItem.ToString().Trim() + "'";
                    string xnk = _open.GetField("虚拟库室", "虚拟库室").ToString().Trim();
                    _open.Reset();
                    _open.ConditionExpress = "部门名称='" + c1.SelectedItem.ToString().Trim() + "'";
                    string gcd = _open.GetField("馆藏地址定义", "馆藏地址").ToString().Trim();
                    string[] s = new string[] { t.Text.Trim(), xnk, gcd };
                    this.list.Add(s);
                }
            }
            DialogResult = DialogResult.OK;
            base.Close();
        }
    }
}
